const express = require('express')

const app = express()

app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'content-type')
    res.header('Access-Control-Allow-Methods', 'DELETE,PUT,POST,GET,OPTIONS')
    if (req.method.toLowerCase() === 'options')
        res.send(200)
    else
        next()
})

var testResp = require('./respData/test.json')
app.get('/test', function (req, res) {
    setTimeout(() => {
        res.status(200).send(testResp);
    }, 0)
})

var shippings = require('./respData/shippings.json')
app.get('/shippings', function (req, res) {
    setTimeout(() => {
        res.status(200).send(shippings);
    }, 0)
})

//get login user Info
var userInfo = require('./respData/userInfo.json')
var userInfo_notFound = require('./respData/userInfo_notFound.json')
app.get('/client/login/:reqData', function (req, res) {
    setTimeout(() => {
        res.status(200).send(userInfo);
    }, 0)

    // setTimeout(() => {
    //     res.status(200).send(userInfo_notFound);
    // }, 0)
})

//get all BWIC
var allBwicList = require('./respData/allBwicList.json')
app.get('/bwic/list/:reqData', function (req, res) {
    setTimeout(() => {
        res.status(200).send(allBwicList);
    }, 0)
})

//get my involved BWIC
var myBwicList = require('./respData/myBwicList.json')
app.get('/bwic/mylist/:reqData', function (req, res) {
    setTimeout(() => {
        res.status(200).send(myBwicList);
    }, 0)
})

//update a BWIC
var updateBwic = require('./respData/updateBwic.json')
var updateBwic_assetsNotEnough = require('./respData/updateBwic_assetsNotEnough.json')
app.post('/bwic/bid', function (req, res) {
    setTimeout(() => {
        res.status(200).send(updateBwic);
    }, 0)
    // setTimeout(() => {
    //     res.status(200).send(updateBwic_assetsNotEnough);
    // }, 0)
})

//cancel a BWIC
var cancelBwic = require('./respData/cancelBwic.json')
app.post('/bwic/cancel', function (req, res) {
    setTimeout(() => {
        res.status(200).send(cancelBwic);
    }, 0)
})

app.listen('5000', function(err) {
    if (err) 
        console.log('Mock server started failed', err)
    else
        console.log('mock server started on http://localhost:5000 successfully!')
})